// pages/self_check/self_check.ts
export { };
const tc_cos = require('../../utils/tc_cos');
const user = require("../../utils/user");
const my_date = require("../../utils/my_date");
const urls = require("../../utils/urls");


Page({

    /**
     * 页面的初始数据
     */
    data: {
        bottle_code_img: "/static/upload_scan_bottle.png",
        app_user_info: { "uuid": "" },
        // job info
        job_date: "",
        job_name: "",
        bottle_code: "未上传",
        valid_bottle_code: false,
        // remote url
        self_check_video_url: "",
        // local path
        self_check_video_path: "",
        help_info: "无",
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad() {
        // get app_user_info
        const app_user_info = wx.getStorageSync("app_user_info");
        // gen job_name
        var job_date = my_date.get_format_date_str();
        var job_name = [app_user_info.uuid, job_date].join('_');
        console.log("log this start");
        console.log(this);
        console.log("log this end");
        setTimeout(() => {
            this.setData({
                "app_user_info": app_user_info,
                "job_name": job_name,
                "job_date": job_date
            })
        }, 0)
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {
        this.setData({});
        console.log("unload");
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },

    choose_bottle_code_img: function (_e: any) {
        let that = this;
        // choose img and upload
        wx.scanCode({
            onlyFromCamera: true,
            scanType: ['barCode', 'qrCode'],
            success(res: any) {
                console.log("scan_bottle_code success");
                console.log(res);
                const full_scan_code: string = res.result;
                const scan_code_prefix = "self_check_bottle_code:";
                if (full_scan_code.startsWith(scan_code_prefix)) {
                    const only_scan_code = full_scan_code.replace(scan_code_prefix, "").trim();
                    that.setData({
                        bottle_code: only_scan_code,
                        valid_bottle_code: true
                    });
                    wx.showToast(
                        {
                            title: '识别成功',
                            icon: 'success',
                            duration: 2000,
                            mask: true
                        }
                    );
                } else {
                    that.setData({
                        bottle_code: "未识别",
                        valid_bottle_code: false
                    });
                    wx.showToast(
                        {
                            title: "无效二维码",
                            icon: 'error',
                            duration: 5000,
                            mask: true
                        }
                    );
                }
            },
            fail(err: any) {
                console.log("scan_bottle_code success");
                console.log(err);
            }
        })
    },
    post_check_bottle_code_img() {
        // fake bottle_code

    },
    shoot_self_check_video() {
        var that = this;
        const job_name = this.data.job_name;
        wx.chooseMedia({
            count: 1,
            mediaType: ['video'],
            sourceType: ['camera'],
            sizeType: ['compressed'],
            maxDuration: 15,
            camera: 'front',
            success(res) {
                let f = res.tempFiles[0]
                console.log(f.tempFilePath);
                console.log("video size");
                console.log(f.size);

                that.setData({
                    self_check_video_path: f.tempFilePath
                })
                // upload videos
                let call_back = (err: any, data: any): void => {
                    console.log("request postObject data");
                    console.log(err);
                    console.log(data);
                    if (err != null) {
                        wx.showToast({
                            title: '上传视频失败',
                            icon: 'success',
                            duration: 3000,
                            mask: true,
                        })
                    } else {
                        wx.showToast({
                            title: '上传视频成功',
                            icon: 'success',
                            duration: 3000,
                            mask: true,
                        })
                    }
                }
                const self_check_video_url = 'videos/' + job_name + "_self_check.mp4";
                that.setData({
                    "self_check_video_url": self_check_video_url
                })
                tc_cos.postObject(self_check_video_url, f.tempFilePath, call_back);
            }
        })

    },
    submit_check_data() {
        let that = this;
        const job_date = this.data.job_date;
        const job_name = this.data.job_name;
        const bottle_code = this.data.bottle_code;
        const video_url = this.data.self_check_video_url;
        const user_uuid = this.data.app_user_info.uuid;

        wx.request({
            url: urls.add_check_task_url,
            method: "POST",
            data: {
                // check_task_dic["uuid"] = request.POST.get("task_uuid", default="")
                // check_task_dic["bottle_id"] = request.POST.get("bottle_code", default="")
                // check_task_dic["video_url"] = request.POST.get("video_url", default="")
                // check_task_dic["video_state"] = request.POST.get("video_state", default="0")
                "task_uuid": job_name,
                "bottle_code": bottle_code,
                "video_url": video_url,
                "upload_time": job_date,
                "user_uuid": user_uuid,
            },
            success(res: any) {
                console.log("add_check_task_url success");
                console.log(res);
                setTimeout(() => {
                    wx.navigateBack();
                }, 4000);
                that.setData({
                    help_info: '检测结果会在送检样本24小时内通知',
                });
                wx.showToast({
                    title: '上传成功',
                    icon: 'success',
                    duration: 5000,
                    mask: true,
                });
            }
        })

    }
})